<template>
  <div>
    <router-view />
    <tabbar v-if="!$route.meta.hiddenTabBar"></tabbar>
  </div>
</template>

<script>
import Tabbar from '@/components/common/Tabbar.vue'
import { mapMutations } from 'vuex'
import { loginMobileRequest, verifyToken } from '@/network/api/user'
export default {
  components: {
    Tabbar
  },
  methods: {
    ...mapMutations('user', ['APP_INIT', 'LOGINSUCCESS'])
  },
  async created() {
    if (localStorage.getItem('userToken')) {
      // token存到vuex
      this.APP_INIT(JSON.parse(localStorage.getItem('userToken')))
    }
    // 先进行token的验证
    const flag = await verifyToken()
    // 发送网络请求 进行登录
    // 拿到用户登录手机号码
    if (flag) {
      if (localStorage.getItem('autoLogin')) {
        const res = await loginMobileRequest(localStorage.getItem('autoLogin'))
        localStorage.setItem('userToken', JSON.stringify(res.token))
        this.LOGINSUCCESS({
          id: res.id,
          user: res.user
        })
        // 拿到购物车数据
        this.$store.dispatch('cart/REQUEST_CART_DATA')
      }
    }
  },
}
</script>
<style>

</style>
